package com.example.shop_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.shop_management.entity.Category;
import com.example.shop_management.entity.Goods;
import com.example.shop_management.entity.Orders;
import com.example.shop_management.service.impl.CategoryServiceImpl;
import com.example.shop_management.service.impl.GoodsServiceImpl;
import com.example.shop_management.service.impl.OrdersServiceImpl;
import com.example.shop_management.service.impl.UserServiceImpl;
import com.example.shop_management.vo.MainVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@RestController
@RequestMapping("main")
public class MainController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private GoodsServiceImpl goodsService;
    @Autowired
    private OrdersServiceImpl ordersService;
    @Autowired
    private CategoryServiceImpl categoryService;
    @Autowired
    private MainVO mainVO;

    @GetMapping("info")
    public MainVO getInfo(){
        Integer userNum = Math.toIntExact(userService.count());
        mainVO.setUserNum(userNum);
        Integer goodsNum = Math.toIntExact(categoryService.count());
        mainVO.setGoodsNum(goodsNum);
        QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
        goodsQueryWrapper.select("sum(inventory) as inventory");
        Goods one = goodsService.getOne(goodsQueryWrapper);
        Integer totalinventory = one.getInventory();
        mainVO.setTotalInventory(totalinventory);
        QueryWrapper<Orders> ordersQueryWrapper = new QueryWrapper<>();
        ordersQueryWrapper.select("sum(sum_price) as sumPrice");
        Orders one1 = ordersService.getOne(ordersQueryWrapper);
        BigDecimal totalSales = one1.getSumPrice();
        mainVO.setTotalSales(totalSales);
//        ArrayList<Map<String, Integer>> goodsInfo = new ArrayList<>();
        Map<String, Integer> goodsInfo = new HashMap<>();
        mainVO.setGoodsInfo(goodsInfo);
        List<Category> list = categoryService.list();
        for (Category category : list) {
            goodsQueryWrapper.clear();
            goodsQueryWrapper.select("sum(inventory) as inventory");
            goodsQueryWrapper.eq("cid", category.getCid());
            Goods one2 = goodsService.getOne(goodsQueryWrapper);

            if (one2 != null) {
                mainVO.getGoodsInfo().put(category.getCname(), one2.getInventory());
            }

        }
//        ArrayList<Map<String, BigDecimal>> salesInfo = new ArrayList<>();
        Map<String, BigDecimal> salesInfo = new HashMap<>();
        mainVO.setSalesInfo(salesInfo);
        List<Goods> goodlist = goodsService.list();
        for (int i= 0;i<goodlist.size();i++){
            ordersQueryWrapper.clear();
            ordersQueryWrapper.select("sum(sum_price) as sumPrice");
            ordersQueryWrapper.eq("gid",goodlist.get(i).getGid());
            Orders one2 = ordersService.getOne(ordersQueryWrapper);

            if(one2!=null){
                mainVO.getSalesInfo().put(goodlist.get(i).getGname(),one2.getSumPrice());
            }

        }
        return mainVO;
    }

}

